package org.travel.app.service.impl;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.travel.app.entity.ConsumePoints;
import org.travel.app.entity.PointsTransactions;
import org.travel.app.entity.spots.Spots;
import org.travel.app.entity.spots.SpotsShopTicket;
import org.travel.app.entity.spots.SpotsTicketPackage;
import org.travel.app.entity.ticket.TicketStock;
import org.travel.app.entity.user.User;
import org.travel.app.entity.user.UserDiscountTicket;
import org.travel.app.entity.spots.SpotsTickets;
import org.travel.app.entity.ticket.TicketDiscount;
import org.travel.app.entity.ticket.TicketPackages;
import org.travel.app.entity.ticket.UserTicketDiscount;
import org.travel.app.mapper.TicketMapper;
import org.travel.app.service.TicketService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author nmm
 * @date 2024-12-2024/12/19
 * @deriction njm......
 */
@Service
@AllArgsConstructor
public class TicketServiceImpl implements TicketService {
    private  final TicketMapper ticketMapper;

    /**
     * 通过景点id查询景点票
     * @param spotsId
     * @return
     */
    @Override
    public List<SpotsTickets> getSpotsTicketById(Integer spotsId) {
        return ticketMapper.getSpotsTicketById(spotsId);
    }

    @Override
    public void reduceStock(int stockUd) {
         ticketMapper.reduceStock(stockUd);
    }

    /**
     * 通过景点编号查询景点套餐
     * @param spotsId
     * @return
     */
    @Override
    public List<TicketPackages> getTicketPackagesBySpotsId(int spotsId) {
        return  ticketMapper.getTicketPackagesBySpotsId(spotsId);
    }

    @Override
    public TicketStock countPackageTicket(int package_id) {
        return ticketMapper.countPackageTicket(package_id);
    }

    @Override
    public void reducePackageStock(int ticketTypeId, int reduceNum) {
        ticketMapper.reducePackageStock(ticketTypeId,reduceNum);
    }

    /**
     * 查询套餐类型
     * @param packageId
     * @return
     */
    @Override
    public int getSpotsPackageChoice(int packageId) {
        return ticketMapper.getSpotsPackageChoice(packageId);
    }

    /**
     * 减不同票种套餐的库存
     * @param packageId
     */
    @Override
    public void getSpotsPackageTicketTypeId(int packageId) {
        reduceDifferentTicketPackage(ticketMapper.getSpotsPackageTicketTypeId(packageId));
    }

    @Override
    public void reduceDifferentTicketPackage(List<Integer> ticketTypesId) {
        System.out.println(ticketTypesId.toString());
         ticketMapper.reduceDifferentTicketPackage(ticketTypesId);
    }

    @Override
    public List<TicketDiscount> getTicketDiscountList() {
        return ticketMapper.getDiscountTicketList();
    }


    /**
     * 判断用户是否有足够的积分(兑换优惠券前)
     * @param transactions
     * @return
     */
    @Override
    public String ensureUserHasPoints(PointsTransactions transactions) {
        String enSure="";
        User user = ticketMapper.ensureUserHasPoints(transactions);
        if (user.getPoints()>= transactions.getPoints()){
            transactions.setCreateAt(new Timestamp(System.currentTimeMillis()));
            //设置兑换时间为当前时间
            transactionTicket(transactions);
            enSure="兑换成功,消费"+transactions.getPoints()+"积分";
        }else {
           enSure= "您的积分不足，请确认后再兑换";
        }
        return  enSure;
    }

    /**
     * 积分交易
     * @param transactions
     */
    @Override
    public void transactionTicket(PointsTransactions transactions) {
        ticketMapper.transactionTicket(transactions);
        //用户兑换优惠券
        addUserTransactionTicket(transactions);
        //添加用户兑换记录

        if (hasDiscountTicket(transactions)==null){
            addUserHasTicket(transactions);
        }else {
            userTransactionTicket(transactions);
        }
        //如果是true则表示第一次兑换优惠券
        //否则是第二次兑换该优惠券
    }

    /**
     * 添加交易记录
     * @param transactions
     */
    @Override
    public void addUserTransactionTicket(PointsTransactions transactions) {
            ticketMapper.addUserTransactionTicket(transactions);
    }


    /**
     * 添加优惠券
     * @param ticketDiscount
     */
    @Override
    public void addTicketDiscount(UserTicketDiscount ticketDiscount) {
        if (ticketDiscount.getDiscountCondition()==0){
            ticketDiscount.setDiscountCondition(null);
        }
        ticketMapper.addTicketDiscount(ticketDiscount);
    }

    /**
     * 用户第一次兑换优惠券
     * @param
     */
    @Override
    public void addUserHasTicket(PointsTransactions transactions) {
          ticketMapper.addUserHasTicket(transactions);
    }

    /**
     * 判断是否有优惠券
     *(返回true表示已经有相同的优惠券应该执行添加数量)
     * @param transactions
     */
    @Override
    public Integer hasDiscountTicket(PointsTransactions transactions) {
         return    ticketMapper.hasDiscountTicket(transactions);
    }


    /**
     * 用户第二次兑换优惠券
     * @param transactions
     */
    @Override
    public void userTransactionTicket(PointsTransactions transactions) {
         ticketMapper.userTransactionTicket(transactions);
    }


    /**
     * 用户使用优惠券(当前用户)
     * @param userDiscountTicket
     */
    @Override
    public void userUsingTicket(UserDiscountTicket userDiscountTicket) {
            ticketMapper.userUsingTicket(userDiscountTicket);
            ConsumePoints consume=new ConsumePoints();
            consume.setUserId(userDiscountTicket.getUserId());
            addUserPoint(consume);
            //当前用户使用优惠券并成功消费(生成订单)
    }

    /**
     * 随机添加用户积分
     * @param consumePoints
     */
    @Override
    public void addUserPoint(ConsumePoints consumePoints) {
        Random random=new Random();
        consumePoints.setPoints(random.nextInt(41) + 10);
        //随机添加积分
        ticketMapper.addUserPoint(consumePoints);
    }

    @Override
    public PageInfo<SpotsShopTicket> getSpotsShopTicketList(int pageNum, int pageSize) {
        List<SpotsShopTicket> spotsShopTicketList = ticketMapper.getSpotsShopTicketList(pageNum, pageSize);
        return new PageInfo<>(spotsShopTicketList);
    }

    /**
     * 查询景点套餐
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<TicketPackages> getSpotsPackage(int pageNum, int pageSize) {
        List<TicketPackages> spotsPackage = ticketMapper.getSpotsPackage(pageNum, pageSize);
        return new PageInfo<>(spotsPackage);
    }

    @Override
    public List<TicketPackages> getCustomSpotsPackage() {
        return ticketMapper.getCustomSpotsPackage();
    }


    /**
     * 查询添加优惠券时用到的套餐
     * @return
     */
    @Override
    public List<TicketPackages> getSpotsPackages() {
        return ticketMapper.getSpotsPackages();
    }

    @Override
    public void addSpotsTicket(SpotsTickets spotsTickets) {
            spotsTickets.setDiscount(null);
            spotsTickets.setIsActive(1);
            spotsTickets.setStockId(spotsTickets.getTypeId());
            ticketMapper.addSpotsTicket(spotsTickets);
    }

    /**
     * 添加套餐
     * @param ticketPackages
     */
    @Override
    public void addTicketPackage(TicketPackages ticketPackages) {
            ticketMapper.addTicketPackage(ticketPackages);
    }

    /**
     * 删除套餐
     * @param packageId
     */
    @Override
    public void delTicketPackage(int packageId) {
          ticketMapper.delTicketPackage(packageId);
    }

    /**
     * 添加套餐关联
     * @param list
     */
    @Override
    public void addSpotsTicketPackage(List<SpotsTicketPackage> list, int ticketNum,int packageId) {
        handlerList(list,ticketNum,packageId);
        ticketMapper.addSpotsTicketPackage(list);
    }

    @Override
    public void addPackSpots(int[] spotsId, int packageId) {
        ticketMapper.addPackSpots(spotsId,packageId);
    }

    public void handlerList(List<SpotsTicketPackage> list,int ticketNum,int packageId) {
        for (SpotsTicketPackage spotsTicketPackage : list) {
            spotsTicketPackage.setPackageId(packageId);
            //设置套餐id
        }
        if (ticketNum>=2){
            //如果是多张同类型的票
            for (int i = 0; i < ticketNum-1; i++) {
                list.add(list.get(i));
            }
        }
    }


    /**
     * 获取用户所有优惠券
     * @param userId
     * @return
     */
    @Override
    public List<UserTicketDiscount> getUserHasTicketList(int userId) {
        return ticketMapper.getUserHasTicketList(userId);
    }

    /**
     * 查询前端优惠券
     * @return
     */
    @Override
    public List<Spots> getDistinctTicketDiscount() {
        return ticketMapper.getDistinctTicketDiscount();
    }

    @Override
    public PageInfo<TicketDiscount> getTicketDiscountsList(int pageNum, int pageSize) {
        List<TicketDiscount> ticketDiscountsList = ticketMapper.getTicketDiscountsList(pageNum, pageSize);
        return new PageInfo<>(ticketDiscountsList);
    }

    /**
     * 获取用户具体的优惠券(对优惠券数量进行优惠券遍历)
     * @return
     */
    public List<UserTicketDiscount> getUserDetailTicket(int userId) {
        List<UserTicketDiscount> userHasTicketList = getUserHasTicketList(userId);
        List<UserTicketDiscount> result = new ArrayList<>();

        for (UserTicketDiscount ticket : userHasTicketList) {
            int quantity = ticket.getQuantity();
            //如果优惠券数量大于0
            if (quantity > 0) {
                addTicketDto(ticket,quantity,result);
            }
        }
        return result;
    }


    /**
     * 遍历出具体的优惠券(根据数量)
     * @param ticket
     * @param quantity
     * @param result
     */
    public void addTicketDto(UserTicketDiscount ticket,int quantity,List<UserTicketDiscount> result) {
        // 添加对应数量的优惠券对象
        for (int i = 0; i <quantity; i++) {
            UserTicketDiscount ticketDto=new UserTicketDiscount();
            BeanUtils.copyProperties(ticket,ticketDto);
            //将查询出来的优惠券信息copy到创建的对象中
            ticketDto.setQuantity(1);
            //设置优惠券数量
            result.add(ticketDto);
            // 每个对象数量为1
        }
    }


}